<?php
/**
 * 查询商品是否是淘宝客商品
 * 查询淘宝客商品，或则重抓淘宝客商品信息
 */
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\cache\wiki\TbkGoods;
use Biz\model\wiki\Wiki;
use Biz\service\wiki\AdmWiki;
use Biz\tools\TbkUnion;

$reload = $argv[1] ?? 'run';
if ($reload == 'run') {
    runTbkGoods();
} else if ($reload == 'reload') {
    reloadTbkGoods();
} else if ($reload == 'offline') {
    $page = $argv[2] ?? 1;
    reloadOfflineTbkGoods($page);
}

function reloadTbkGoods()
{
    var_dump('reload tbk goods info');
    $list = [];
    $page = 1;
    $pageSize = 500;
    do {
        $list = Wiki::selectByWhere([
                'channel' => Wiki::CHANNEL_TAOBAO,
                'LIMIT' => [($page - 1) * $pageSize, $pageSize],
                'status[!]' => Wiki::STATUS_DEL,
                'ORDER' => ['id' => 'DESC']
            ]
        );
        $itemIds = array_column($list, 'channel_spu_id');
        $tbkList = TbkGoods::isTbkGoods($itemIds);
        foreach ($list as $row) {
            if (isset($tbkList[$row['channel_spu_id']])) {
                var_dump('重新抓取淘宝客商品信息#wiki_id:' . $row['id'] . '#淘宝商品id:' . $row['channel_spu_id']);
                try {
                    AdmWiki::admReloadWiki($row, AdmWiki::RELOAD_WIKI_TBK);
                } catch (\Exception $e) {
                    var_dump($e->getMessage());
                }
            }
        }
        $page++;
    } while ($list);
}

/**
 * @param int $reload 重新抓取淘宝客商品基础信息
 * @throws Exception
 */
function runTbkGoods()
{
    var_dump('run tbk goods');
    $list = [];
    $page = 1;
    $pageSize = 2000;
    do {
        $list = Wiki::selectByWhere([
            'channel' => Wiki::CHANNEL_TAOBAO,
            'LIMIT' => [($page - 1) * $pageSize, $pageSize],
            'status[!]' => Wiki::STATUS_DEL,
            'ORDER' => ['id' => 'DESC']
        ], 'channel_spu_id'
        );
        foreach ($list as $itemId) {
            $itemInfo = TbkUnion::getItemInfo($itemId);
            if (empty($itemInfo)) {
                for ($i = 2; $i < 4; $i++) {
                    if (empty($itemInfo)) {
                        $itemInfo = TbkUnion::getItemInfo($itemId);
                    } else {
                        break;
                    }
                }
            }
            if ($itemInfo) {
                var_dump('商品id:' . $itemId . '为淘宝客商品');
                TbkGoods::addGoods($itemId);
            } else {
                var_dump('商品id:' . $itemId . '查询3次，未能查询到淘宝客信息');
                TbkGoods::delGoods($itemId);
            }
        }
        $page++;
    } while ($list);
}

function reloadOfflineTbkGoods($page = 1)
{
    $pageSize = 1000;
    $list = Wiki::selectByWhere([
            'channel' => Wiki::CHANNEL_TAOBAO,
            'channel_spu_status' => Wiki::CHANNEL_SPU_STATUS_OFF,
            'ORDER' => ['id' => 'DESC'],
            'LIMIT' => [($page - 1) * $pageSize, $pageSize],
        ]
    );
    var_dump(Wiki::log(), count($list));
    $itemIds = array_column($list, 'channel_spu_id');
    $tbkList = TbkGoods::isTbkGoods($itemIds);
    foreach ($list as $row) {
        if (isset($tbkList[$row['channel_spu_id']])) {
            var_dump('重新抓取淘宝客商品信息#wiki_id:' . $row['id'] . '#淘宝商品id:' . $row['channel_spu_id']);
            try {
                $res = AdmWiki::admReloadWiki($row, AdmWiki::RELOAD_WIKI_TBK);
                if ($res) {
                    var_dump('重新抓取成功');
                }
            } catch (\Exception $e) {
                var_dump($e->getMessage());
            }
        }
    }
}
